package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.model.BaseCategoryTrademark;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.atguigu.gmall.product.model.CategoryTrademarkVo;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类品牌中间表 业务实现类
 *
 * @author atguigu
 * @since 2023-04-18
 */
@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTrademarkService {


    @Autowired
    private BaseTrademarkService baseTrademarkService;

    /**
     * 根据category3Id获取品牌列表
     *
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> getTrademarkListByCategoryId(Long category3Id) {
        //根据category3Id查询所有品牌的id集合
        //BaseCategoryTrademark 实体
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        //获取集合对象
        List<BaseCategoryTrademark> baseCategoryTrademarkList = this.list(queryWrapper);
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            //获取品牌的id
            List<Long> tradeMarkdList = baseCategoryTrademarkList.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            //根据品牌id集合查询品牌集合
            List<BaseTrademark> baseTrademarkList = baseTrademarkService.listByIds(tradeMarkdList);
            return baseTrademarkList;
        }

        return null;

    }

    @Override
    public List<BaseTrademark> getCurrentTrademarkList(Long category3Id) {
        //第一种方式,两次单表查询 1查询品牌所有,2 查询中间表获取当前分类下的所有id ,再stream流进行过滤

        //第二种方式 ,采用自定义sql实现查询(子查询,关联查询)

        BaseTrademarkMapper trademarkBaseMapper = (BaseTrademarkMapper) baseTrademarkService.getBaseMapper();

        List<BaseTrademark> baseTrademarkList = trademarkBaseMapper.getCurrentTrademarkList(category3Id);
        return baseTrademarkList;
    }

    @Override
    public void saveCategoryTrademark(CategoryTrademarkVo categoryTrademarkVo) {
        //获取关联到当前分类品牌id的几个 转换为 分类品牌关系对象集合
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        if (!CollectionUtils.isEmpty(trademarkIdList)) {
            //判断id集合是不是空
            //取出遍历trademarkId
            List<BaseCategoryTrademark> categoryTrademarkList = trademarkIdList.stream().map(trademarkId -> {
                //创建中间表 BaseCategoryTrademark
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                //添加Category3Id和TrademarkId
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                baseCategoryTrademark.setTrademarkId(trademarkId);
                return baseCategoryTrademark;
            }).collect(Collectors.toList());   //转成集合数据
            //批量保存
            this.saveBatch(categoryTrademarkList);
        }
    }

    @Override
    public void deleteCategoryTrademark(Long category3Id, Long trademarkId) {
        //通过BaseCategoryTrademark获取category3Id和trademarkId
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        queryWrapper.eq(BaseCategoryTrademark::getTrademarkId, trademarkId);

        //逻辑删除is_deleted=1
        //物理删除 delete from  where id=id
        this.remove(queryWrapper);
    }
}
